package com.dao.impl;

import com.dao.ChartRedisDao;
import com.dao.RedisGeneratorDao;
import com.entity.Chart;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;


@Repository(value = "chartRedisDao")
public class ChartRedisDaoImpl extends RedisGeneratorDao implements ChartRedisDao {

    /**
     * 添加对象
     */
    @Override
    public boolean add(final Chart chart) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(chart.getTypeName());
                byte[] name = serializer.serialize(chart.getName());
                return connection.setNX(key, name);
            }
        });
        return result;
    }

    /**
     * 添加集合
     */
    @Override
    public boolean add(final List<Chart> list) {
        Assert.notEmpty(list);
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                for (Chart chart : list) {
//                    byte[] key  = serializer.serialize(chart.getPid());
//                    byte[] name = serializer.serialize(chart.getPname());
//                    byte[] key  = serializer.serialize(chart.getPid());
//                    byte[] key  = serializer.serialize(chart.getPid());
                    //connection.setNX(key, name);
                }
                return true;
            }
        }, false, true);
        return result;
    }



    /**
     * 修改对象
     */
    public boolean update(final Chart chart) {
//        String key = chart.getPid();
//        if (get(key) == null) {
//            throw new NullPointerException("数据行不存在, key = " + key);
//        }
//        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
//            public Boolean doInRedis(RedisConnection connection)
//                    throws DataAccessException {
//                RedisSerializer<String> serializer = getRedisSerializer();
//                byte[] key  = serializer.serialize(chart.getPid());
//                byte[] name = serializer.serialize(chart.getPname());
//                connection.set(key, name);
//                return true;
//            }
//        });
        //return result;
        return false;
    }

    /**
     * 根据key获取对象
     */
    @Override
    public Chart get(final String keyId) {
        Chart result = redisTemplate.execute(new RedisCallback<Chart>() {
            public Chart doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(keyId);
                byte[] value = connection.get(key);
                if (value == null) {
                    return null;
                }
                String nickname = serializer.deserialize(value);
                // return new Chart(keyId, nickname);
                return null;
            }
        });
        return result;
    }

    @Override
    public List<Chart> getCharts(String typeName) {
        List<Chart> charts = redisTemplate.execute(new RedisCallback<List<Chart>>() {
            public  List<Chart> doInRedis(RedisConnection connection)
                    throws DataAccessException {
                GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(typeName);
                byte[] value = connection.get(key);
                //默认的序列化器反序列化
                //return (List<Chart>)new JdkSerializationRedisSerializer().deserialize(value);
                return (List<Chart>)jackson2JsonRedisSerializer.deserialize(value);
            }
        },true,false);
        return charts;
    }

    public boolean pushCharts(List<Chart> charts,String typeName) {
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(typeName);
                //使用默认的jdk序列化器 存放在redis无法解读，但不影响反序列化
                //byte[] name = new JdkSerializationRedisSerializer().serialize(charts);
                byte[] name = jackson2JsonRedisSerializer.serialize(charts);
                connection.set(key, name);
                System.out.println(key+" "+name);
                return true;
            }
        },true,false);
        return result;
    }
}